import type { AxiosResponse } from "axios";
import axiosInstance from "./axios";

interface ApiResponse<T> {
	code?: number;
	success?: boolean;
	message?: string;
	result: T;
}

const apiGet = async (url: string, params?: any) => {
	try {
		const response = await axiosInstance.get(url, { params });
		return response;
	} catch (error) {
		const errorMsg = (error as TObj)?.message || "API GET request failed";
		throw new Error(errorMsg); // 统一抛出错误
	}
};

const apiPost = async (url: string, params?: any) => {
	try {
		const response = await axiosInstance.post(url, params);
		return response;
	} catch (error) {
		// console.error('POST request error:', error)
		throw new Error((error as any)?.message || "POST request failed");
	}
};
const apiPatch = async (url: string, params?: any) => {
	try {
		const response = await axiosInstance.patch(url, params);
		return response;
	} catch (error) {
		// console.error('PATCH request error:', error)
		throw new Error((error as any)?.message || "PATCH request failed");
	}
};

const apiDelete = async (url: string, params?: any) => {
	try {
		const response = await axiosInstance.delete(url, { data: params });
		return response;
	} catch (error) {
		throw new Error((error as any)?.message || "DELETE request failed");
	}
};

export { apiGet, apiPost, apiPatch, apiDelete };
